package com.baizw.cultivation.module.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baizw.cultivation.module.admin.entity.DictDataEntity;
import com.baizw.cultivation.module.admin.mapper.DictDataMapper;
import com.baizw.cultivation.module.admin.page.DictDataSearch;
import com.baizw.cultivation.module.admin.service.DictDataService;
import com.baizw.cultivation.util.DictUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典数据接口实现
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年06月29日 11:46
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataEntity> implements DictDataService {

    @Override
    public IPage<DictDataEntity> pageByParams(DictDataSearch<DictDataEntity> page) {
        return baseMapper.pageByParams(page);
    }

    @Override
    public List<DictDataEntity> listByType(String dictType) {
        List<DictDataEntity> dictDataList = DictUtils.getDictCache(dictType);
        if (CollUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        LambdaQueryWrapper<DictDataEntity> queryWrapper = Wrappers.<DictDataEntity>lambdaQuery()
                .eq(DictDataEntity::getDictType, dictType);
        List<DictDataEntity> dataEntityList = super.list(queryWrapper);
        if (CollUtil.isNotEmpty(dataEntityList)) {
            DictUtils.setDictCache(dictType, dataEntityList);
        }
        return dataEntityList;
    }
}
